package com.example.demo.controller;

import com.example.demo.dao.GoodsMapper;
import com.example.demo.service.GoodsService;
import com.example.demo.vo.Goods;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;



    /**
     * 货物管理页面
     *
     * @return
     */

    @RequestMapping("/goods")
    public String goods(Model m,
                        @RequestParam(value = "start",defaultValue = "1")int start,
                        @RequestParam(value = "size",defaultValue = "5")int size) {
        // 将查询的数据以分页的形式展示
        PageHelper.startPage(start,size,"id desc");// 还可以指定排序规则
        List<Goods> list = goodsService.findGoodsList(new Goods());
        PageInfo<Goods> page = new PageInfo<>(list);
        m.addAttribute("page",page);
        return "goods";
    }

    @RequestMapping("/findGoodsList")
    @ResponseBody
    public List<Goods> findGoodsList(Goods goods) {
        List<Goods> list = goodsService.findGoodsList(goods);
        return list;
    }

    @RequestMapping("/addGoods")
    public String addCategory(Goods c){
        goodsService.insert(c);
        return "redirect:goods";
    }
    @RequestMapping("/newGoods")
    public String newGoods(String id, Model mode){
        Goods vo = new Goods();
        if (!StringUtils.isEmpty(id)) {
            vo.setId(id);
            List<Goods> list = goodsService.findGoodsList(vo);
            vo = list.get(0);
        }
        mode.addAttribute("form",vo);
        return "newGoods";
    }
}
